<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
       <el-form-item label="训练地名称" prop="testSiteName">
         <el-input
             v-model="queryParams.testSiteName"
             placeholder="请输入训练地名称"
             clearable
             style="width: 200px"
             @keyup.enter="handleQuery"
         />
       </el-form-item>
       <el-form-item label="是否启用" prop="status">
        <el-select v-model="queryParams.status" placeholder="是否启用" filterable clearable style="width: 200px">
          <el-option
            v-for="dict in dict.type.comm_yesno_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['member:certificateTrainingSite:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="考点名称" align="center" prop="testSiteName"/>
      <el-table-column label="省" align="center" prop="provinceName"/>
      <el-table-column label="市" align="center" prop="cityName"/>
      <el-table-column label="区" align="center" prop="countyName"/>
      <el-table-column label="详细地址" align="center" prop="address"/>
      <el-table-column label="是否启用" align="center" prop="status">
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.comm_yesno_status" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)"
            v-hasPermi="['member:certificateTrainingSite:edit']">修改
          </el-button>
          <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)"
            v-hasPermi="['member:certificateTrainingSite:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改计费规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="考点名称" prop="testSiteName">
          <el-input v-model="form.testSiteName" placeholder="请输入考点名称"/>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="省份" prop="provinceCode">
              <el-select v-model="form.provinceCode" @change="proviceChange" clearable filterable  placeholder="请选择省份">
                <el-option
                  v-for="item in proviceList"
                  :key="item.areaCode"
                  :label="item.name"
                  :value="item.areaCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="市"  prop="cityCode">
              <el-select v-model="form.cityCode" @change="cityChange" clearable filterable placeholder="请选择市">
                <el-option
                  v-for="item in cityList"
                  :key="item.areaCode"
                  :label="item.name"
                  :value="item.areaCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="区" prop="countyCode">
              <el-select v-model="form.countyCode" clearable filterable placeholder="请选择区">
                <el-option
                  v-for="item in countyList"
                  :key="item.areaCode"
                  :label="item.name"
                  :value="item.areaCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.comm_yesno_status" :label="Number(dict.value)" :key="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="popLodding" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/pilot/certifTrainDest";
import { listByPCode, } from "@/api/address/area";
export default {
name: "CertifTrainDest",
dicts: ['comm_yesno_status',],
components: {  },
data() {
  return {
    popLodding:false,//弹窗加载
    form:{},
    // 弹出层标题
    title: "",
    // 是否显示弹出层
    open: false,
    // 遮罩层
    loading: true,
    // 显示搜索条件
    showSearch: true,
    queryParams: {// 查询参数
      pageNum: 1,testSiteName:null,status:null,
      pageSize: 10,
    },
    // 总条数
    total: 0,
    rules: {
      testSiteName: [{required: true, message: "考点名称不能为空", trigger: "blur"}],
      provinceCode: [ { required: true, message: "省不能为空", trigger: "change" } ],
      cityCode: [ { required: true, message: "市不能为空", trigger: "change" } ],
      // countyCode: [ { required: true, message: "区不能为空", trigger: "change" } ],
      address: [{required: true, message: "详细地址不能为空", trigger: "blur"}],
      status: [{required: true, message: "是否启用不能为空", trigger: "change"}],
    },
    list:[],//页面数据
    proviceList:[],//省
    cityList:[],//市
    countyList:[],//区
  };
},
created() {
  this.getList()
},
methods: {
  proviceChange(e){// 省份下拉改变
    let form = {...this.form}
    form.cityCode = ''
    form.countyCode = ''
    this.form = {...form}
    this.cityList = []
    this.countyList = []
    this.initLocationOptions(e,2)
  },
  cityChange(e) {// 市下拉改变
    let form = {...this.form}
    form.countyCode = ''
    this.form = {...form}
    this.countyList = []
    this.initLocationOptions(e,3)
  },
  initLocationOptions(code,type) {// 初始化位置选项
    listByPCode(code).then(resp => {
      if(type == 1) this.proviceList = resp.data || []
      if(type == 2) this.cityList = resp.data || []
      if(type == 3) this.countyList = resp.data || []
    })
  },
  getList() {
    this.loading = true;
    api.listCertificateTrainingSite(this.queryParams).then(response => {
      this.list = response.rows || [];
      this.total = response.total || 0;
      this.loading = false;
    });
  },
  handleAdd() {
    this.open = true;
    this.reset();
    this.title = "添加飞员考证训练目的地";
  },
  handleUpdate(row) {
    this.reset();
    const id = row.id || this.ids;
    this.open = true;
    this.title = "修改飞员考证训练目的地";
    api.getCertificateTrainingSite(id).then(response => {
      let data = response.data || {}
      let provinceCode = data.provinceCode || ''
      let cityCode = data.cityCode || ''
      let countyCode = data.countyCode || ''
      this.form = {
        ...data,
        provinceCode:provinceCode ? Number(provinceCode) : '',
        cityCode:cityCode ? Number(cityCode) : '',
        countyCode:countyCode ? Number(countyCode) : '',
      };
      this.initLocationOptions(data.provinceCode,2);
      this.initLocationOptions(data.cityCode,3);
    });
  },
  submitForm() {
    this.$refs["formRef"].validate(valid => {
      if (valid) {
        let form = {...this.form}
        let obj = this.proviceList.find(item=>item.areaCode == form.provinceCode)
        form.provinceName = obj ? obj.name : form.provinceCode
        let obj1 = this.cityList.find(item=>item.areaCode == form.cityCode)
        form.cityName = obj1 ? obj1.name : form.cityCode
        let obj2 = this.countyList.find(item=>item.areaCode == form.countyCode)
        form.countyName = obj2 ? obj2.name : form.countyCode
        this.popLodding = true
        if (this.form.id != undefined) {
          api.updateCertificateTrainingSite(form).then(response => {
            this.popLodding = false
              this.$modal.msgSuccess("修改成功")
              this.open = false;
              this.getList();
          }).catch(error=>this.popLodding = false);
        } else {
          api.addCertificateTrainingSite(form).then(response => {
            this.popLodding = false
              this.$modal.msgSuccess("新增成功")
              this.open = false;
              this.getList()
          }).catch(error=>this.popLodding = false);
        }
      }
    });
  },
  handleDelete(row) {//删除
    const del_ids = row.id || this.ids;
    this.$modal.confirm('是否确认删除该数据项？').then(function() {
      return api.delCertificateTrainingSite(del_ids);
    }).then(() => {
      this.getList();
      this.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  },
  // 表单重置
  reset() {
    this.form = {status:1,};
    this.resetForm("formRef");
    this.initLocationOptions(0,1);
  },
  /** 搜索按钮操作 */
  handleQuery() {
    this.queryParams.pageNum = 1;
    this.getList();
  },
  /** 重置按钮操作 */
  resetQuery() {
    this.resetForm("queryForm");
    this.handleQuery();
  },
}
};
</script>
